﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess;
using baseSet;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;
using dhsystem.Main;


namespace dhsystem.produceForm
{
    public partial class FrmcgshrkMasterAdd : Form
    {
        purchase.cgshrkDetailDataTable cgshrkDetailTable = new purchase.cgshrkDetailDataTable();
        
        cgshrkBLL cgshrk = new cgshrkBLL();
        deptBLL dept = new deptBLL();
        workerListBLL workerList = new workerListBLL();
        cgddBLL cgdd = new cgddBLL();
        supplierListBLL supplier = new supplierListBLL();
        //cgshrkMasterModel cgshrkMasterRow = new cgshrkMasterModel();
        purchase.cgshrkMasterDataTable cgshrkMasterTable = new purchase.cgshrkMasterDataTable();
        BindingSource b2;
        string state;
        string stockName;
        public FrmcgshrkMasterAdd(BindingSource b1, string _state, string _stockName)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
            stockName = _stockName;
        }
        //打开窗口时绑定数据
        private void FrmcgshrkMasterAdd_Load(object sender, EventArgs e)
        {
            textstockname.Text = stockName;
            
            textbillacount.Text ="0";
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptName.DataSource = bindingSourcedeptList;
            comboBoxdeptName.DisplayMember = "deptName";
            comboBoxdeptName.ValueMember = "deptNum";
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";
            bindingSource1.DataSource = workerList.getworkerListAll();
            comboBoxwrokernameck.DataSource = bindingSource1;
            comboBoxwrokernameck.DisplayMember = "name";
            comboBoxwrokernameck.ValueMember = "workerNum";
            bindingSourcesupplierList.DataSource = supplier.GetDatasupplierListBysTypeNo();
            comboBoxsNum.DataSource = bindingSourcesupplierList;
            comboBoxsNum.DisplayMember = "sName";
            comboBoxsNum.ValueMember = "sNum";
            if (state == "新增")
            {
                textbillNo.Text = "R" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("采购收货入库单号");
                bindingSourcecgshrkDetail.DataSource = cgshrk.getscgshrkDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcecgshrkDetail;

            }
            if (state == "编辑")
            {
                textbillNo.DataBindings.Add("text", b2, "billNo");
                comboBoxdeptName.DataBindings.Add("SelectedValue", b2, "deptName");
                comboBoxworkername.DataBindings.Add("SelectedValue", b2, "workername");
                comboBoxwrokernameck.DataBindings.Add("SelectedValue", b2, "wrokernameck");
                textckbillno.DataBindings.Add("text", b2, "ckbillno");
                dateTimebillDate.DataBindings.Add("value", b2, "billDate");
               // textbillacount.DataBindings.Add("text", b2, "billacount");
                comboBoxsNum.DataBindings.Add("SelectedValue", b2, "sNum");
                textmark.DataBindings.Add("text", b2, "mark");
                textbillStatus.DataBindings.Add("text", b2, "billStatus");
                bindingSourcecgshrkDetail.DataSource = cgshrk.getscgshrkDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcecgshrkDetail;
            }
            DcolumnSet.colSet(moduleInfo.cgshrkDetail, dGridDetail);
            //dGridDetail.AllowUserToAddRows = false;
            dGridDetail.AllowUserToAddRows = true;
           // dGridDetail.Columns[3].ReadOnly = true;
            this.KeyPreview = true;
            if (stockName == "原材料仓")
            {
                comboBoxworkername.Text = "钟三妹";
            }
            if (stockName == "原材料仓")
            {
                comboBoxwrokernameck.Text = "黄青云";
            }
            else
            {
                comboBoxworkername.Text = "张忠平";
            }
            comboBoxdeptName.Text = "仓储部";
        }
        //int i = 0;
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillStatus .Text  == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                       dhDataAcess.dataAcess.purchase.cgshrkDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.cgshrkDetailDataTable)bindingSourcecgshrkDetail.DataSource;                     //  purchase.cgshrkDetailDataTable cgshrkDetailTable = new purchase.cgshrkDetailDataTable();
                       foreach (purchase.cgshrkDetailRow row in ptable)
                        {
                            if (row.IsfqtyNull ())
                            {
                                row.fqty = 0;
                            }
                            var s = ptable.Where(x => x.matNum == row.matNum).ToList();
                            if (s.Count()>1 )
                            {
                                MessageBox.Show("存在两条相同原料名称的记录，不能保存");
                                return;
                            }
                            if (!row.IsqtyNull())
                            {
                                row.qty = row.qty;
                            }
                            else
                            {
                                DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                                if (sresult == DialogResult.Yes)
                                {
                                    row.qty = 0;
                                }
                                if (sresult == DialogResult.No)
                                {
                                    MessageBox.Show("保存已取消");
                                    return;
                                }
                            }
                        }
                        purchase.cgshrkMasterRow cgshrkMasterRow = cgshrkMasterTable.NewcgshrkMasterRow();
                        cgshrkMasterRow.billNo = textbillNo.Text;
                        cgshrkMasterRow.deptName = comboBoxdeptName.SelectedValue.ToString();
                        cgshrkMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        cgshrkMasterRow.wrokernameck = comboBoxwrokernameck.SelectedValue.ToString();
                        cgshrkMasterRow.ckbillno = textckbillno.Text;
                        cgshrkMasterRow.billDate = dateTimebillDate.Value;
                        cgshrkMasterRow.billStatus = "";
                        cgshrkMasterRow.billcount  = Convert.ToDecimal(textbillacount.Text);
                        cgshrkMasterRow.sNum = comboBoxsNum.SelectedValue.ToString();
                        cgshrkMasterRow.createDate = DateTime.Now;
                        cgshrkMasterRow.createUser = GlobalInfo.UserName;
                        cgshrkMasterRow.updateDate = DateTime.Now;
                        cgshrkMasterRow.updateUser = GlobalInfo.UserName;
                        cgshrkMasterRow.mark = textmark.Text;
                        cgshrkMasterRow.stockname = textstockname.Text;
                        cgshrkMasterTable.AddcgshrkMasterRow(cgshrkMasterRow);
                        cgshrkMasterTable.AcceptChanges();
                        cgshrk.cgshrkInsert(cgshrkMasterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcecgshrkMaster.DataSource = cgshrk.getcgshrkMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmcgshrkDetailAdd cgshrkDetail = new FrmcgshrkDetailAdd(bindingSourcecgshrkDetail, "新增", textbillNo.Text);
            cgshrkDetail.ShowDialog();
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {
            FrmcgshrkDetailAdd cgshrkDetail = new FrmcgshrkDetailAdd(bindingSourcecgshrkDetail, "编辑", textbillNo.Text);
            cgshrkDetail.ShowDialog();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            if (textbillStatus.Text == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    
                    if (state=="编辑")
                    {
                        string billStatus = dGridDetail.CurrentRow.Cells["billStatus"].Value.ToString();
                        if(billStatus== "结算")
                        {
                        MessageBox.Show("此订单已经结算，不能删除。如需删除请反结算");
                        return;
                        }
                    }
                    DialogResult result = MessageBox.Show("是否保存信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        dhDataAcess.dataAcess.purchase.cgshrkDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.cgshrkDetailDataTable)bindingSourcecgshrkDetail.DataSource;                     //  purchase.cgshrkDetailDataTable cgshrkDetailTable = new purchase.cgshrkDetailDataTable();
                        foreach (purchase.cgshrkDetailRow row in ptable)
                        {
                            if (row.IsfqtyNull())
                            {
                                row.fqty = 0;
                            }
                            var s = ptable.Where(x => x.matNum == row.matNum).ToList();
                            if (s.Count()>1 )
                            {
                                MessageBox.Show("存在两条相同原料名称的记录，不能保存");
                                return;
                            }
                            if (!row.IsqtyNull())
                            {
                                row.qty = row.qty;
                            }
                            else
                            {
                                DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                                if (sresult == DialogResult.Yes)
                                {
                                    row.qty = 0;
                                }
                                if (sresult == DialogResult.No)
                                {
                                    MessageBox.Show("保存已取消");
                                    return;
                                }
                            }
                        }
                        purchase.cgshrkMasterRow cgshrkMasterRow = cgshrkMasterTable.NewcgshrkMasterRow();
                        cgshrkMasterRow.billNo = textbillNo.Text;
                        cgshrkMasterRow.deptName = comboBoxdeptName.SelectedValue.ToString();
                        cgshrkMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        cgshrkMasterRow.wrokernameck = comboBoxwrokernameck.SelectedValue.ToString();
                        cgshrkMasterRow.ckbillno = textckbillno.Text;
                        cgshrkMasterRow.billDate = dateTimebillDate.Value;
                        cgshrkMasterRow.billStatus = "";
                        cgshrkMasterRow.billcount = Convert.ToDecimal(textbillacount.Text);
                        cgshrkMasterRow.sNum = comboBoxsNum.SelectedValue.ToString();
                        cgshrkMasterRow.createDate = DateTime.Now;
                        cgshrkMasterRow.createUser = GlobalInfo.UserName;
                        cgshrkMasterRow.updateDate = DateTime.Now;
                        cgshrkMasterRow.updateUser = GlobalInfo.UserName;
                        cgshrkMasterRow.mark = textmark.Text;
                        cgshrkMasterRow.stockname = textstockname.Text;
                        cgshrkMasterTable.AddcgshrkMasterRow(cgshrkMasterRow);
                        cgshrkMasterTable.AcceptChanges();
                        cgshrk.cgshrkInsert(cgshrkMasterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcecgshrkMaster.DataSource = cgshrk.getcgshrkMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //双击原材料所在行单元格事件
        private void dGridDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string field;
            field = "matNum";
            int colIndex = dGridDetail.Columns["matNum"].Index;
            int colIndexmark = dGridDetail.Columns["mark"].Index;
            if (colIndex == e.ColumnIndex)
            {
                Frmprodutselect select = new Frmprodutselect(moduleInfo.materialList , field);
                DialogResult result = select.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridDetail.CurrentCell.Value = select.pname;
                }
            }
            if (colIndexmark == e.ColumnIndex)
            {
                Frmmark mark = new Frmmark();
                DialogResult result = mark.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridDetail.CurrentCell.Value = mark.mark;
                }
            }
        }
        //选择入库人
        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }
        //选择入库部门
        private void buttuserselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "deptNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.deptList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxdeptName.SelectedValue = productselect.pname;
            }
        }
        //选择采购人
        private void button4_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxwrokernameck.SelectedValue = productselect.pname;
            }
        }
        //选择供应商
        private void button2_Click(object sender, EventArgs e)
        {
            string field;
            field = "sNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.supplierList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxsNum.SelectedValue = productselect.pname;
            }
        }
        //选择采购订单
        private void button3_Click(object sender, EventArgs e)
        {
            string field;
            field = "billNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.cgddMaster , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textckbillno .Text = productselect.pname;
            }
        }
        //采购单号发生改变之后
        private void textckbillno_TextChanged(object sender, EventArgs e)
        {
            purchase.cgddDetailDataTable cgddTable = cgdd.getscgddDetailAllBybillNo(textckbillno.Text);
          
            foreach (purchase.cgddDetailRow cgddRow in cgddTable)
            {
                purchase.cgshrkDetailRow cgshrkRow=cgshrkDetailTable.NewcgshrkDetailRow();
                cgshrkRow.acount = cgddRow.acount;
                cgshrkRow.billNo = textbillNo.Text;
                cgshrkRow.createDate = DateTime.Now;
                cgshrkRow.currency = cgddRow.currency;
                cgshrkRow.qty = cgddRow.qty;
                cgshrkRow.price = cgddRow.price;
                cgshrkRow.fqty = cgddRow.fqty;
                cgshrkRow.funit = cgddRow.funit;
                cgshrkRow.unit = cgddRow.unit;
                cgshrkRow.matNum = cgddRow.matNum;
                cgshrkRow.matName = cgddRow.matName;
                cgshrkRow.matSpec = cgddRow.matSpec;
                cgshrkRow.mark = cgddRow.mark;
                cgshrkDetailTable.AddcgshrkDetailRow(cgshrkRow);
            }
            bindingSourcecgshrkDetail.DataSource = cgshrkDetailTable;
            dGridDetail.DataSource = bindingSourcecgshrkDetail ;          
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            state = "新增";
            cgshrkMasterTable.Clear();
            textbillNo.Text = "R" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("采购收货入库单号");
            bindingSourcecgshrkDetail.DataSource = cgshrk.getscgshrkDetailAllBybillNo(textbillNo.Text);
            dGridDetail.DataSource = bindingSourcecgshrkDetail;
            dGridDetail.AllowUserToAddRows = true;
            // dGridDetail.Columns[3].ReadOnly = true;
            this.KeyPreview = true;
            comboBoxworkername.Text = "钟三妹";
            comboBoxdeptName.Text = "仓储部";
            dateTimebillDate.Value = DateTime.Now;
        }

        private void toolDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
